//
//  YHYStyleCollectionCell.m
//  Home
//
//  Created by 杨泓晔 on 16/6/18.
//  Copyright © 2016年 LanJue. All rights reserved.
//

#import "YHYStyleCollectionCell.h"
#import "YHYDetailPictureCollectionViewCell.h"
@interface YHYStyleCollectionCell ()<UICollectionViewDataSource,UICollectionViewDelegate>{
    NSInteger index;
    CGFloat _itemWH;
    CGFloat _margin;
    NSMutableArray *_StrArr;
     NSMutableArray *_keyNameArray;
    NSMutableDictionary *parameters;
    NSString * personStyle;
    NSString * name;

    
    NSMutableDictionary *styleDic;

}

@property (nonatomic, strong)UILabel *xmLabel;
@property (nonatomic, strong)UICollectionViewFlowLayout *layout;
@property (nonatomic, strong)UICollectionView *myCollectionView;
@property (nonatomic, strong)NSMutableArray *keyArray;
@property (nonatomic, strong)NSMutableArray *didArray;
@property (nonatomic, strong)NSMutableArray *arr;

@end


@implementation YHYStyleCollectionCell
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatView];
    }
    return self;
}
- (void)creatView{
    _arr = [NSMutableArray arrayWithObjects:@"h1",@"h2",@"h3",@"h4",@"h5",@"h6",@"h7",@"h8",@"h9",@"h10",@"h11", @"h12",nil];
    _didArray = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12", nil];
     _StrArr = [NSMutableArray arrayWithObjects:@"美式乡村",@"欧式古典",@"时尚混搭",@"北欧",@"中式",@"法式",@"日式",@"现代",@"田园",@"东南亚",@"地中海",@"其他", nil];

    self.layout = [[UICollectionViewFlowLayout alloc] init];
    _margin = 5 * AUTO_HEIGHT;
    _itemWH = (SCREEN_WIDTH - 20 * AUTO_WIDTH-2 * _margin - 7) / 3 - _margin;
    self.layout.itemSize = CGSizeMake(327/4 * AUTO_WIDTH  , 225/4  * AUTO_HEIGHT+22 *AUTO_HEIGHT);
    self.layout.minimumInteritemSpacing = 10 * AUTO_WIDTH;
    self.layout.minimumLineSpacing = 10 * AUTO_WIDTH;
    _myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10 * AUTO_WIDTH, 10 * AUTO_WIDTH, SCREEN_WIDTH-20 *AUTO_WIDTH, _itemWH * 3 - 45 * AUTO_HEIGHT) collectionViewLayout:self.layout];
    
    //    裁掉view的边界
    //    裁掉view的边界
    self.myCollectionView.layer.masksToBounds = NO;
    //    设置半径
    //self.myCollectionView.layer.cornerRadius = 6 * AUTO_WIDTH;
    self.myCollectionView.scrollEnabled = NO;
    self.myCollectionView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.myCollectionView];
    [_myCollectionView registerClass:[YHYDetailPictureCollectionViewCell class] forCellWithReuseIdentifier:@"YHYDetailPictureCollectionViewCell"];
      _myCollectionView.dataSource = self;
    _myCollectionView.delegate = self;
    
     _keyArray = [NSMutableArray array];
    _keyNameArray = [NSMutableArray array];
    
    [_myCollectionView reloadData];
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _arr.count;
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    index = [NSIndexPath indexPathForItem:indexPath.row inSection:indexPath.section];
    YHYDetailPictureCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YHYDetailPictureCollectionViewCell" forIndexPath:indexPath];
    cell.myImageView.image = [UIImage imageNamed:[_arr objectAtIndex:indexPath.row]];
    
    if (_keyArray != nil) {
        if ([_keyArray containsObject:cell.myImageView.image]) {
            cell.myImageView.image = [UIImage imageNamed:[_didArray objectAtIndex:indexPath.row]];

        }
            }
    
    else{
        cell.myImageView.image = [UIImage imageNamed:[_arr objectAtIndex:indexPath.row]];
    }

    
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    YHYDetailPictureCollectionViewCell *cell = (YHYDetailPictureCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];

    cell.myImageView.image = [UIImage imageNamed:[_arr objectAtIndex:indexPath.row]];
    cell.imageName = [_StrArr objectAtIndex:indexPath.row];
    if ([_keyArray containsObject:[UIImage imageNamed:[_arr objectAtIndex:indexPath.row]]]) {
        [_keyArray removeObject:cell.myImageView.image];
    }
    if ([_keyNameArray containsObject:_StrArr[indexPath.row]]) {
        [_keyNameArray removeObject:cell.imageName];
    }
    //添加
    else{
        
        if (_keyArray.count < 6 ||_keyNameArray.count < 6) {
            
        [_keyArray addObject:cell.myImageView.image];
            [_keyNameArray addObject:cell.imageName];
            
        }
        else{
    
    if (_keyArray.count >= 6 || _keyNameArray.count >= 6) {
        [self.deleagte presentnum];
        
     
    }

        }

}
    for (personStyle in _keyNameArray) {
        NSLog(@"nnnnnn=%@",personStyle);

    }

    


    //  _keyNameArray 擅长风格
    
    [self.deleagte postArray:_keyNameArray];
    
    


    //  _keyNameArray 擅长风格
    

    [_myCollectionView reloadItemsAtIndexPaths:@[indexPath]];

}



-(void)postPersonInformation{
   
    NSLog(@"ssss=%@",personStyle);
    NSString * uid = [LOCALSEVE objectForKey:@"UID"];
    NSString * code = [LOCALSEVE objectForKey:@"MCODE"];
    NSString * url_string = [NSString stringWithFormat:@"%@%@",JIAFANURL,@"Home/CreateHome"];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = HTTPSET;
    [manager POST:url_string parameters:@{@"uid":uid,@"code":code,@"style":_keyNameArray} constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
//        NSLog(@"data%@",formData);
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        
        NSLog(@"ssssssss=%@",responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"errror   ===  = = = %@",error);
        
        
    }];
    


    [self.deleagte postArray:_keyNameArray];

    
   
    
    
    
//    [_myCollectionView reloadItemsAtIndexPaths:@[indexPath]];

}
-(void)setSelectArr:(NSMutableArray *)selectArr{

    if (selectArr.count == 0) {
        
    }
    
    else {
        styleDic = [NSMutableDictionary new];
        for (int i = 0 ; i < _arr.count; i++ ) {
            [styleDic setObject:_didArray[i] forKey:_StrArr[i]];
        }
 
            for (NSString *string  in selectArr) {
                [_didArray insertObject:[styleDic objectForKey:string] atIndex: _didArray.count];
            }
        for (NSString *string  in selectArr) {
            [_keyArray insertObject:[styleDic objectForKey:string] atIndex: _keyArray.count];
        }
      
    }
    
    
    
    [self.myCollectionView reloadData];
}

//
//-(void)postPersonInformation{
//   
//    NSLog(@"ssss=%@",personStyle);
//    NSString * uid = [LOCALSEVE objectForKey:@"UID"];
//    NSString * code = [LOCALSEVE objectForKey:@"MCODE"];
//    NSString * url_string = [NSString stringWithFormat:@"%@%@",JIAFANURL,@"Home/CreateHome"];
//    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//    manager.responseSerializer.acceptableContentTypes = HTTPSET;
//    [manager POST:url_string parameters:@{@"uid":uid,@"code":code,@"style":_keyNameArray} constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//        
////        NSLog(@"data%@",formData);
//    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        
//        
//        NSLog(@"ssssssss=%@",responseObject);
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"errror   ===  = = = %@",error);
//        
//        
//    }];
//    
//
//
//}


/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
